package Model;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Vector;

import javax.swing.table.AbstractTableModel;

import Dao.ProjectManageDao;
import Entity.Project;;

public class ProjectManageModel extends AbstractTableModel{
	private static final long serialVersionUID = 1L;
	private List<Project> projectList = new Vector<Project>();
	private ProjectManageDao dao = new ProjectManageDao();
	private String[] header = { "STT", "Project Name", "Project Allowance" };
	
	public void loadProjectList() throws IOException, ClassNotFoundException, SQLException{
		projectList = dao.getProjectList();
	}
	public int getColumnCount() {
		// TODO Auto-generated method stub
		return header.length;
	}

	public int getRowCount() {
		// TODO Auto-generated method stub
		return projectList.isEmpty() == true ? 0 : projectList.size();
	}
	
	public String getColumnName(int column) {
		// TODO Auto-generated method stub
		return header[column];
	}
	
	public List<Project> getProjectList() {
		return projectList;
	}
	
	public Object getValueAt(int rowIndex, int columnIndex) {
		// TODO Auto-generated method stub
		if (projectList.isEmpty()) {
			return null;
		}
		Project project = projectList.get(rowIndex);
		switch (columnIndex) {
		case 0:
			return rowIndex + 1;
		case 1:
			return project.getName();
		case 2:
			return project.getAllowance();
		}
		return null;
	}

	public void addProject(Project project) throws IOException, ClassNotFoundException, SQLException{
		projectList.add(project);
		dao.insert(project);
		fireTableDataChanged();
	}

	public void editProject(int row, Project project) throws IOException, ClassNotFoundException, SQLException {
		projectList.set(row, project);
		dao.update(project);
		fireTableDataChanged();
	}

	public void deleteProject(int row, Project project) throws IOException, ClassNotFoundException, SQLException {
		projectList.remove(row);
		dao.delete(project);
		fireTableDataChanged();
	}
}
